package com.wuercloud.http.json.codec;

import com.wuercloud.utils.FastJsonUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.Charset;

/**
 * Created by JiangWL on 2017/2/14.
 */
public abstract class AbstractHttpJsonEncoder<T> extends MessageToMessageEncoder<T> {
    private static Logger log = LoggerFactory.getLogger(AbstractHttpJsonEncoder.class);
    final static Charset UTF_8 = Charset.forName("utf-8");

    protected ByteBuf encode0(ChannelHandlerContext ctx, Object body) {
        //String jsonStr = FastJsonUtils.serialize(body);
        String jsonStr = FastJsonUtils.objectToJson(body);
        log.info("请求提交内容：" + jsonStr);
        ByteBuf encodeBuf = Unpooled.wrappedBuffer(jsonStr.getBytes(UTF_8));
        //ByteBuf encodeBuf = Unpooled.copiedBuffer(jsonStr, UTF_8);
        return encodeBuf;
    }

}
